// 数据模型实例
package chess

import (
	"kugo/chess/proto"
	"kugo/log"
	"kugo/utils"
)

type serverModel struct {
	//棋盘大小
	GridSize int
	//用户数据
	Users []proto.UserInfo
	//房间号
	RoomId int
	//棋盘数据
	ChessData [][]int
	//当前操作Id
	CurSeatId int
	//游戏状态
	CurGameState int
	//中心两点
	CenterPoint []proto.ChessPoint
}

var ServerModel *serverModel = &serverModel{}

func (sm *serverModel) Init() {
	sm.GridSize = 8
	sm.RoomId = 1001
	sm.Users = []proto.UserInfo{}
	sm.CurSeatId = SeatType_WHITE
	sm.CurGameState = GameState_WAIT

	sm.CenterPoint = []proto.ChessPoint{
		{Row: 3, Col: 3},
		{Row: 4, Col: 4},
	}
	sm.resetChessPieces()
}
func (sm *serverModel) setTestData() {
	sm.ChessData = [][]int{
		{2, 3, 2, 3, 2, 3, 2, 3},
		{3, 2, 3, 2, 3, 2, 3, 2},
		{2, 3, 2, 3, 2, 3, 2, 3},
		{3, 2, 3, 1, 3, 2, 3, 2},
		{2, 3, 2, 3, 1, 3, 2, 3},
		{3, 2, 3, 2, 3, 2, 3, 2},
		{2, 3, 2, 3, 2, 3, 2, 3},
		{3, 2, 3, 2, 3, 2, 3, 2},
	}
}

// 重置棋盘
func (sm *serverModel) resetChessPieces() {
	sm.ChessData = make([][]int, sm.GridSize)
	for row := 0; row < sm.GridSize; row++ {
		sm.ChessData[row] = make([]int, sm.GridSize)
		for col := 0; col < sm.GridSize; col++ {
			sm.ChessData[row][col] = ChessPieces_NONE
		}
	}
}

// 有没有空位
func (sm *serverModel) HaveNoneSeat() bool {
	return len(sm.Users) < PLAYER_MAX
}

func (sm *serverModel) AddUser(userId string, headUrl string, nickName string) *proto.UserData {
	var user = sm.GetUserInfoById(userId)
	if user != nil {
		return user.Data
	}

	if !sm.HaveNoneSeat() {
		return nil
	}
	var seatId = len(sm.Users) + 1
	var userData = &proto.UserData{}
	userData.HeadUrl = headUrl
	userData.NickName = nickName
	userData.UserId = userId
	userData.Score = 66666

	var userInfo = proto.UserInfo{}
	userInfo.SeatId = seatId
	userInfo.Data = userData
	userInfo.PiecesType = seatId + 1
	userInfo.LeftTime = 25 * 60
	userInfo.ReadyState = ReadyState_WAIT

	sm.Users = append(sm.Users, userInfo)

	return userData
}

func (sm *serverModel) GetUserInfoById(userId string) *proto.UserInfo {
	log.Info("GetUserInfoById【 %s 】data: %s", userId, utils.Struct2JSON(sm.Users))
	for i := 0; i < len(sm.Users); i++ {
		if sm.Users[i].Data.UserId == userId {
			return &sm.Users[i]
		}
	}
	return nil
}

func (sm *serverModel) JoinRoom(userId string, roomId int) *proto.UserInfo {
	userInfo := sm.GetUserInfoById(userId)
	if userInfo == nil {
		return nil
	}
	userInfo.RoomId = roomId
	return userInfo
}

// 返回是否都准备完成
func (sm *serverModel) UserReady(userId string) bool {
	userInfo := sm.GetUserInfoById(userId)
	userInfo.ReadyState = ReadyState_READY
	if sm.readyCount() < PLAYER_MAX {
		return false
	} else {
		sm.CurGameState = GameState_DROP_PIECES
		return true
	}
}

// 准备了的玩家数量
func (sm *serverModel) readyCount() int {
	var count = 0
	for i := 0; i < len(sm.Users) && i < PLAYER_MAX; i++ {
		if sm.Users[i].ReadyState == ReadyState_READY {
			count++
		}
	}
	return count
}

// 改变操作位置
func (sm *serverModel) ChangeOptUser(userId string) {
	userInfo := sm.GetUserInfoById(userId)
	if userInfo.SeatId == SeatType_BLACK {
		sm.CurSeatId = SeatType_WHITE
	} else {
		sm.CurSeatId = SeatType_BLACK
	}
}

// 落子
func (sm *serverModel) Drop(userId string, dropPoint *proto.ChessPoint, killPoints []*proto.ChessPoint) bool {
	if sm.ChessData[dropPoint.Row][dropPoint.Col] != ChessPieces_NONE {
		return false
	}
	userInfo := sm.GetUserInfoById(userId)
	if killPoints != nil {
		for i := 0; i < len(killPoints); i++ {
			sm.ChessData[killPoints[i].Row][killPoints[i].Col] = ChessPieces_NONE
		}
	}
	var piecesType = userInfo.PiecesType
	sm.ChessData[dropPoint.Row][dropPoint.Col] = piecesType
	return true
}
